import { ChartItemProps, ChartType, ChartPropsType } from "type"

class Register {
  items: ChartItemProps
  constructor() {
    this.items = {}
  }
  register<T>(key: string, value: T) {
    this.items[key] = value as T
    return this
  }
  has(key: string): boolean {
    const item = this.items[key]
    return item !== null && item !== undefined
  }
  keys(): string[] {
    return Object.keys(this.items)
  }

  getAll(keys?: ChartType | ChartType[]) {
    if (keys) {
      if (typeof keys === 'string') {
        if (keys && !this.has(keys)) throw new Error(`请先用new XXX().register('${keys}')初始化 ‘${keys}’`)
      } else {
        keys?.forEach((key) => {
          if (key && !this.has(key)) throw new Error(`请先用new XXX().register('${key}')初始化 ‘${key}’`)
        })
      }
    }
    return this.items
  }

  getChart(key: 'line'): typeof this.items.line
  getChart(key: 'pie'): typeof this.items.pie
  getChart(key: 'map'): typeof this.items.map
  getChart(key: 'bar'): typeof this.items.bar
  getChart(key: 'map-echart'): typeof this.items['map-echart']
  getChart(key: ChartType): ChartPropsType {
    if (key && !this.has(key)) throw new Error(`请先用new XXX().register('${key}')初始化 ‘${key}’`)
    return this.items[key]
  }

  clear() {
    this.items = {}
    return this
  }
  remove(key: string) {
    if (!this.has(key)) throw new Error(`‘${key}’未初始化`)
    delete this.items[key]
    return this
  }
}

export default Register